package com.feeyo.prophet.actions.common;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.feeyo.llm.agent.engine.Action;
import com.feeyo.llm.agent.engine.ActionCategory;
import com.feeyo.llm.agent.engine.ActionResult;
import com.feeyo.llm.agent.engine.ExecuteOptions;
import com.feeyo.prophet.pojo.action.fleet.FleetAttributesResp;
import com.feeyo.prophet.plugins.AnswerUtils;
import com.feeyo.prophet.plugins.DbSearchActionUtils;
import com.feeyo.prophet.plugins.FleetAttributesActionUtils;
import com.google.common.collect.Maps;
import io.micrometer.common.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;

import java.util.Comparator;
import java.util.List;
import java.util.Map;

/**
 * 用于计算节假日
 * 节假日获取连接:
 * https://timor.tech/api/holiday/year/2021
 *
 * 1. 数据计算      -> 问题, 计算类不强制走这个action
 * 2. 节假日计算    -> 待开发
 * 3. 返回的链接    -> 需要增加标题, 说明
 */
@Slf4j
public class HolidayCheckerAction implements Action {
    @Override
    public String human() {
        return "中国节假日计算";
    }

    @Override
    public String name() {
        return "HolidayCheckerAction";
    }

    @Override
    public String eg() {
        return """
                Question: 2024年五一节假日是什么日期
                Thought: 我应该使用HolidayCheckerAction查找节假日日期
                Action: HolidayCheckerAction
                Action Input: {"year":"2024"}
                """;
    }

    @Override
    public ActionCategory[] category() {
        return new ActionCategory[]{ActionCategory.COMMON, ActionCategory.DICTIONARY};
    }

    @Override
    public String description() {
        return "Calculate Chinese holidays";
    }

    @Override
    public String parameters() {
        return """
		{
			"type": "object",
			"properties": {
				"data": {
					"type": "number",
					"description": "year",
				}
			}
		}
		""";
    }

    @Override
    public ActionResult run(String question, String input, ExecuteOptions options) throws Exception {
        try {
            JSONObject req = JSON.parseObject(input);
            String year = req.getString("year");
            return getHoliday(year, input);
        } catch (Throwable e) {
            log.error("error", e);
            return new ActionResult(HttpStatus.INTERNAL_SERVER_ERROR.value(), "节假日计算错误");
        }
    }


    private ActionResult getHoliday(String year, String input) {
        String respTxt = null;
        try {
            respTxt = DbSearchActionUtils.getHoliday(year);
        } catch (Exception e) {
            log.error("HolidayCheckerAction error", e);
        }
        if (StringUtils.isEmpty(respTxt)) {
            return new ActionResult(HttpStatus.INTERNAL_SERVER_ERROR.value(), "日期查询发生错误，请稍后再试");
        }
        Map<String, Map<String, Object>> holidayMap = getHolidayMap(respTxt);

        List<FleetAttributesResp> operatorList = FleetAttributesActionUtils.getOperatorDetail(respTxt);
        operatorList.sort(Comparator.comparing(FleetAttributesResp::getAircraftStatus));
        if (operatorList.isEmpty()) {
            return new ActionResult(HttpStatus.BAD_REQUEST.value(), "机场数据不存在，请稍后再用");
        }
        return AnswerUtils.answer(name(), input, JSON.toJSONString(operatorList));
    }

    public static Map<String, Map<String, Object>> getHolidayMap(String year) {
        Map<String, Map<String, Object>> holidayMap = Maps.newHashMap();
        return holidayMap;
    }
}
